package com.xlinur.android.moviecatalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.xlinur.moviegrabber.Grabber;
import com.xlinur.moviegrabber.GrabberFactory;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MovieGrab extends Activity {
	private static final int DIALOG_PROGRESS = 1;
	private static List<String> mEntriesText = new ArrayList<String>();
	private static List<String> mEntriesId = new ArrayList<String>();
	private static EditText eSearch;
	private static ListView eList;
	private static Map<String,String> mInfo;
	private static Grabber mGrabber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.movie_grab);
		eSearch = (EditText) findViewById(R.id.editEditText_title);
		Button buttonSearch = (Button) findViewById(R.id.button_search);
		buttonSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				searchMovie();
			}
		});
		eList = (ListView) findViewById(R.id.list);
		eList.setOnItemClickListener(new ListView.OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
				try {
					getMovieInfo(mEntriesId.get(pos));
				} catch(IndexOutOfBoundsException  e){
				}
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		eSearch.setText(MovieEditor.mTitle);
		SharedPreferences prefs = PreferenceManager
			.getDefaultSharedPreferences(getBaseContext());
		String grabber = prefs.getString("prefGrabber", "imdb.com");
		mGrabber = GrabberFactory.getInstance().getGrabber(grabber);
		searchMovie();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		super.onCreateDialog(id);
		switch (id) {
		case DIALOG_PROGRESS:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage(getResources().getString(
					R.string.grab_progress_message));
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		default:
			return null;
		}
	}

	private void searchMovie() {
		showDialog(DIALOG_PROGRESS);
		new Thread(new Runnable() {
			public void run() {
				final boolean complete = getListMovies();
				MovieGrab.this.runOnUiThread(new Runnable() {
					public void run() {
						removeDialog(DIALOG_PROGRESS);
						if (complete) {
							showList();
						} else {
							Toast.makeText(getApplicationContext(),
									R.string.grab_progress_fail,
									MovieCatalog.MESSAGE_TIME).show();
						}
					}
				});
			}
		}).start();

	}
	
	private void getMovieInfo(final String id) {
		showDialog(DIALOG_PROGRESS);
		new Thread(new Runnable() {
			public void run() {
				final boolean complete = getMovie(id);
				MovieGrab.this.runOnUiThread(new Runnable() {
					public void run() {
						removeDialog(DIALOG_PROGRESS);
						if (complete) {
							if (mInfo!=null){
								MovieEditor.mInfo = mInfo;
							} else {
								MovieEditor.mInfo.clear();
							}
							finish();
						} else {
							Toast.makeText(getApplicationContext(),
									R.string.grab_progress_fail,
									MovieCatalog.MESSAGE_TIME).show();
						}
					}
				});
			}
		}).start();
	}

	private void showList(){
		eList.setAdapter(new ArrayAdapter<String>(getBaseContext(),
				android.R.layout.simple_list_item_1, mEntriesText));
	}
	
	private Boolean getMovie(String id) {
		mInfo = mGrabber.getItem(id);
		return mInfo!=null;
	}
	private Boolean getListMovies() {
		HashMap<String, String> list = mGrabber.getList(eSearch.getText().toString());
		mEntriesText.clear();
		mEntriesId.clear();
		if (list!=null){
			@SuppressWarnings("rawtypes")
			Set set = list.entrySet();
			@SuppressWarnings("rawtypes")
			Iterator it = set.iterator();
			while(it.hasNext()){
				@SuppressWarnings("unchecked")
				Map.Entry<String, String> entry = (Entry<String, String>)it.next();
				mEntriesText.add(entry.getValue());
				mEntriesId.add(entry.getKey());
			}
		}
		return list!=null;
	}
}
